package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.MsgUser;
import com.qf.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController{
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/all",method = RequestMethod.GET )
    @ResponseBody     /*返回为json串*/
    public MsgUser<User> selectAll(){
        List<User> list=userService.selectA11();
        MsgUser<User> msgUser=new MsgUser<>();
        msgUser.setCode(0);
        msgUser.setData(list);
        msgUser.setCount(list.size());
//        msgUser.setMsg("success");
  /*      System.out.println("msgUser = " + msgUser);*/
        return msgUser;
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public MsgUser update(User user){
        /*System.out.println("user = " + user);*/
        int ss=userService.update(user);
        System.out.println("ss = " + ss);
        MsgUser msg=new MsgUser();
        msg.setMsg("修改成功");
        msg.setCode(ss);
        return msg;
    }
    @ResponseBody
    @RequestMapping(value = "/delete/{idArry}",method =RequestMethod.DELETE)
    public MsgUser delete(@PathVariable("idArry") String idArry){
        String ids[]=idArry.split(",");
        System.out.println("ids = " + ids);
        List alist=new ArrayList();
        /*int[] uids=new int[ids.length];*/
        for(int i=0;i<ids.length;i++){
            alist.add(Integer.valueOf(ids[i]));
        }
        int res=userService.delete(alist);
        MsgUser msg=new MsgUser();
        msg.setMsg("删除成功");
        msg.setCode(res);
        return msg;
    }
    @RequestMapping("/add")
    public String  insert(User user){

        int res=userService.insert(user);
        return "admin";
    }
    @RequestMapping("/selectOne/{u_id}")
    @ResponseBody
    public MsgUser<User> selectOne(@PathVariable("u_id") int id){
        MsgUser<User> msgUser=new MsgUser<>();
        User user=userService.selectOne(id);
        List<User> list=new ArrayList<>();
        list.add(user);
        msgUser.setData(list);
        return msgUser;
    }
    @RequestMapping("/login")
    public String login(String name,String pass, HttpSession session){
        User loginUser=userService.selectOneByNamePassword(name,pass);

        if(loginUser!=null){
            session.setAttribute("loginUser",loginUser);
            if(loginUser.getU_role()==1){
                return "admin";
            }else
                return "show";
        }else{
            return "login";
        }
    }
    @RequestMapping(value = "/addShow",method = RequestMethod.POST) //注册
    public String  insertShow(User user){
        int res=userService.insertShow(user);

        return "login";
    }
    public String loginOut(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session=req.getSession();
        session.invalidate();
        return "show";
    }
}
